<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); ?>

<?php 
	$ci = & get_instance();
	$tid = intval($_GET['tid']);
	$page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0 ;
	$records_per_page = TOP_RANKS_PER_PAGE;
	$rank = $page + 1;
	
	$TOURNAMENT = $ci->tournament_entity->init("id", $tid);
	$numberTopRank = $TOURNAMENT->getNumberTopRank();
	
	//$rank_array = $ci->user_tournament_entity->getArrayListOfTournament($tid);
	//$prizeListArray = $ci->prize_entity->getArrayListOfPrize($tid);
	
	$week_prize_array = $ci->week_prize_entity->getLiveWeekPrizeOfTournament($tid);
	
	if(!isset($_SESSION['sys_prize_type'])){
		$_SESSION['sys_prize_type'] = 'weekly';
	}
	
	$content = '';
	
	if($_SESSION['sys_prize_type'] != 'weekly'){
		$total = count( $ci->db->where( array("tournament_id"=>$tid) )->get(TBL_USER_TOURNAMENT)->result() );
		$total = $total <= $numberTopRank ? $total : $numberTopRank;
		
		$pagination = create_pagination( 
									$uri = 'cricket/contentload/top_rank/?tid='.$tid, 
									$total_rows = $total , 
									$limit= $records_per_page,
									$uri_segment = 0,
									TRUE, TRUE 
								);
							 
		$records = $this->db->where(array("tournament_id"=>$tid, 'point >'=> 0) )->order_by('point','desc')->limit($records_per_page, $page )->get(TBL_USER_TOURNAMENT)->result();
		
		foreach($records as $item){
			$USER =findUserWithRank($rank);
			$PRIZE = getPrizeObject($rank, $tid);
			$thumb = $uname = $points = $rankicon = '';
			if($USER){
				$thumb = thumb($USER->getMyAvatar(),USER_TOP_RANK_W,USER_TOP_RANK_H, true); 
				$uname = $USER->getUserName();
				$points = bet_format( $ci->user_tournament_entity->getMyPointInTournament($USER->getId(),$tid) );
			}
			$worth = '';
			if($PRIZE){
				$worth = "Worth ".$PRIZE->getValue();
			}
			 
			$rankicon = getRankIcon($rank,$tid, "style='float:left;margin-bottom:0px;margin-top:5px;text-align:center;'",60,40);
		//  if($USER AND $PRIZE){
				$content .= "
						<div class=\"rowitem2 rank-wrap\">
							<div class=\"grid2 rankshow-wrap\">
								<div class=\"rank-show\">{$rank} </div>
								<div class=\"avatar-show\">
									<div class=\"avatar-wrap\">
										$thumb
										</br>
										$uname
									</div>
								</div>
							</div>
							
							<div class=\"grid1 pointshow\">
								<div class=\"point-wrap\">
									{$points}
								</div>
							</div>
							<div class=\"grid2 rankprize\" >
								<div class=\"icon-wrap\">
									{$rankicon}
								</div>
								<span>{$worth}</span>
							</div>
							<div class=\"clrboth\"></div>
						</div>
						<div class=\"clrboth\"></div>";
						$rank++;
		//	}
		}	
	}else{
		if($week_prize_array){
			$cat_info = $week_prize_array['cat_info'][0];
			
			$allRanksArray = $ci->week_prize_entity->getArrayWeeklyRanksOfTournament($tid,$cat_info->id);
			$total = count($allRanksArray);
			$total = $total <= $numberTopRank ? $total : $numberTopRank;
			$pagination = create_pagination( 
									$uri = 'cricket/contentload/top_rank/?tid='.$tid, 
									$total_rows = $total , 
									$limit= $records_per_page,
									$uri_segment = 0,
									TRUE, TRUE 
								);
			$tmpArr = array_chunk($allRanksArray, $records_per_page, true);
			$index= intval($page/$records_per_page);
			$records = $tmpArr[$index];
		}else{
			$pagination['links'] ='';
			$records = array();
		}
		
		foreach($records as $item){
			$USER = $ci->week_prize_entity->getUserObjectWithRank($rank,$tid,$cat_info->id);
			$PRIZE = $ci->week_prize_entity->getPrizeObjectWithARank($rank, $cat_info->id);
			$thumb = $uname = $points = $rankicon = '';
			if($USER){
				$userInfo = $ci->week_prize_entity->getPersonWithARank($rank,$tid,$cat_info->id);
				$thumb = thumb($USER->getMyAvatar(),USER_TOP_RANK_W,USER_TOP_RANK_H, true); 
				$uname = $USER->getUserName();
				$points = bet_format( $userInfo['point'] );
			}
			$worth = '';
			$prizeImage = '';
			if($PRIZE){
				$worth = "Worth ".$PRIZE->getValue();
				$prizeImage = $PRIZE->getPrizeImage();
			}
			$ext =  "style='float:left;margin-bottom:0px;margin-top:5px;text-align:center;'";
			if($prizeImage){
				$rankicon = "<img src='".thumb2($prizeImage,PRIZE_TOP_RANK_W,PRIZE_TOP_RANK_H)."' $ext />";
			}
		//	if($USER AND $PRIZE){
				$content .= "
						<div class=\"rowitem2 rank-wrap\">
							<div class=\"grid2 rankshow-wrap\">
								<div class=\"rank-show\">{$rank} </div>
								<div class=\"avatar-show\">
									<div class=\"avatar-wrap\">
										$thumb
										</br>
										$uname
									</div>
								</div>
							</div>
							
							<div class=\"grid1 pointshow\">
								<div class=\"point-wrap\">
									{$points}
								</div>
							</div>
							<div class=\"grid2 rankprize\" >
								<div class=\"icon-wrap\">
									{$rankicon}
								</div>
								<span>{$worth}</span>
							</div>
							<div class=\"clrboth\"></div>
						</div>
						<div class=\"clrboth\"></div>";
						$rank++;
		//	}
		}	
			
	}	
	
	
	function findUserWithRank($rank){
		$ci = & get_instance();
		$tid = intval($_GET['tid']);
		$rank_array = $ci->user_tournament_entity->getArrayListOfTournament($tid);
		if(count($rank_array) < $rank){return null;}
		if( $rank_array[$rank-1]->point){
			$user_id = $rank_array[$rank-1]->user_id;
			return clone_($ci->user_entity->init('id', $user_id));
		}
		return null;
	}
	function getMyPoint($user_id){
		$ci = & get_instance();
		$tid = intval($_GET['tid']);
		$rank_array = $ci->user_tournament_entity->getArrayListOfTournament($tid);
		$p = null;
		foreach($rank_array as $item){
			if($item->user_id == $user_id){
				$p = $item->point;
				break;
			}
		}	
		if($p > 0 ) return $p;
		return null;
	}
	
	$html = $content;	
	$html .= "<div id=\"pagination_top_rank\">".$pagination['links']."</div> ";
	
	echo $html;
	 
?>	